package ru.urfc.portal.ui.client.googlets.admin;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Style;
import ru.urfc.portal.ui.client.controllers.ItemsController;
import ru.urfc.portal.ui.client.controllers.events.PutEvent;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.renderers.MoneyTextRenderer;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;

public class GlobalNotifications extends BaseGooglet {

	interface Binder extends UiBinder<Widget, GlobalNotifications> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField Style style;
	@UiField Element name;
	@UiField CheckBox deviceCertificateExpiring;
	@UiField CheckBox netLicenseExpiring;
	@UiField ComboBox<ModelData> expirationNotifyInterval;
	@UiField ComboBox<ModelData> invoiceUnpaidDays;
	@UiField ComboBox<ModelData> invoiceUnpaidNotifyInterval;
	
	public Type<ItemsController> SAVE = new Type<ItemsController>();

	@UiHandler("save")
	void changeAccess(ClickEvent e) {
		getEventsDispatcher().fireEvent(new PutEvent(SAVE));
	}
	
	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		name.setInnerText("Настройка уведомлений");
		initComboBox(expirationNotifyInterval);
		initComboBox(invoiceUnpaidDays);
		initComboBox(invoiceUnpaidNotifyInterval);
		
		GridDescriptor descriptor = new GridDescriptor(NOTIFICATION_SETTINGS, GLOBAL_NOTIFICATION_SETTINGS);
		descriptor.addBoolField("deviceCertificateExpiring", deviceCertificateExpiring);
		descriptor.addBoolField("netLicenseExpiring", netLicenseExpiring);
		descriptor.addSelectField("expirationNotifyInterval", expirationNotifyInterval);
		descriptor.addSelectField("invoiceUnpaidDays", invoiceUnpaidDays);
		descriptor.addSelectField("invoiceUnpaidNotifyInterval", invoiceUnpaidNotifyInterval);
		register(SAVE, createController(descriptor));		
	}

	static public ListStore<ModelData> initComboBox(ComboBox<ModelData> comboBox) {
		ListStore<ModelData> store = new ListStore<ModelData>();
		for (int i = 1; i <= 20; ++i) {
			store.add(createModelData(i));
		}
		comboBox.setStore(store);
		comboBox.setDisplayField("text");
		comboBox.setValueField("code");
		comboBox.setTriggerAction(TriggerAction.ALL); 
		return store;
	}
	
	static private ModelData createModelData(Integer day) {
		ModelData  model = new BaseModel();
		model.set("code", day.toString());
		model.set("text", MoneyTextRenderer.renderDay(day));
		return model;
	}

}